package xeroxware.framework.message;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
 * <pre>
 * ExtendMessageSource
 * </pre>
 * 
 * @since 2012. 12. 24.
 * @author XEROXER
 */
public class ExtendMessageSource extends ResourceBundleMessageSource implements MessageSource {

	private String[] sources;

	public ExtendMessageSource() {
		super();
	}

	public void setSource(String _source) {
		sources = new String[] { _source };

		setBasename(_source);
	}

	public void setSources(String[] _source) {
		sources = _source;

		setBasenames(_source);
	}

	/**
	 * 메시지 정보를 Map 형태로 반환한다.
	 * 
	 * @return Map 형태의 메시지 정보
	 */
	public Map<String, Object> getMessageSourceMap() {
		return getMessageSourceMap(Locale.getDefault());
	}

	/**
	 * 메시지 정보를 Map 형태로 반환한다.
	 * 
	 * @param locale
	 * @return Map 형태의 메시지 정보
	 */
	public Map<String, Object> getMessageSourceMap(Locale locale) {

		Map<String, Object> map = new HashMap<String, Object>();
		ResourceBundle resourceBundle = null;
		Enumeration<String> msgEnum = null;
		String key = null;

		for (String baseName : sources) {
			resourceBundle = getResourceBundle(baseName, locale);
			msgEnum = resourceBundle.getKeys();

			while (msgEnum.hasMoreElements()) {
				key = msgEnum.nextElement();

				map.put(key, resourceBundle.getString(key));
			}
		}

		return map;
	}
}
